package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /*
     * @Description  新增套餐
     * @Param [setmealDTO]
     * @return com.sky.result.Result
     **/
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
     setmealService.saveWithDish(setmealDTO);
     return Result.success();
    }

    /*
     * @Description  分页
     * @Param [setmealPageQueryDTO]
     * @return com.sky.result.Result<com.sky.result.PageResult>
     **/
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){

       PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /*
     * @Description  数据回显
     * @Param [id]
     * @return com.sky.result.Result<com.sky.vo.SetmealVO>
     **/
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
      SetmealVO setmealVO=setmealService.getByIdWithDish(id);
       return Result.success(setmealVO);
    }

    /*
     * @Description  修改套餐信息
     * @Param [setmealDTO]
     * @return com.sky.result.Result
     **/
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /*
     * @Description  修改状态
     * @Param [status, id]
     * @return com.sky.result.Result
     **/
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result StartOrStop(@PathVariable Integer status,Long id){
        Setmeal  setmeal=new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealService.updateStatus(setmeal);
        return Result.success();
    }
}
